﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ESupervision.Models;
using System.Data.Entity.Infrastructure;

namespace ESupervision.Controllers
{
    public class AttachementFileController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: /AttachementFile/
        public async Task<ActionResult> Index()
        {
            return View(await db.AttachmentFiles.ToListAsync());
        }

        // GET: /AttachementFile/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            // Student student = db.Students.Include(s => s.Files).SingleOrDefault(s => s.ID == id);

            AttachementFile attachementfile = await db.AttachmentFiles.FindAsync(id);

            if (attachementfile == null)
            {
                return HttpNotFound();
            }
            return View(attachementfile);
        }

        // GET: /AttachementFile/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /AttachementFile/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        //public async Task<ActionResult> Create([Bind(Include="AttachementFileID,FileName,ContentType,Content")] HttpPostedFileBase upload,AttachementFile attachementfile)
        public ActionResult Create([Bind(Include = "AttachementFileID,FileName,ContentType,Content,FileType")] HttpPostedFileBase upload, AttachementFile attachementfile)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (upload != null && upload.ContentLength > 0)
                    {
                        AttachementFile avatar = new AttachementFile
                        {
                            FileName = System.IO.Path.GetFileName(upload.FileName),
                            ContentType = upload.ContentType
                        };
                        using (var reader = new System.IO.BinaryReader(upload.InputStream))
                        {
                            avatar.Content = reader.ReadBytes(upload.ContentLength);
                        }
                        attachementfile.Content = avatar.Content;
                        attachementfile.FileName = avatar.FileName;
                        attachementfile.ContentType = avatar.ContentType;
                    }
                    db.AttachmentFiles.Add(attachementfile);
                    db.SaveChanges();
                    //await db.SaveChangesAsync();
                    return RedirectToAction("Index");
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            return View(attachementfile);
        }

        // GET: /AttachementFile/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            AttachementFile attachementfile = await db.AttachmentFiles.FindAsync(id);
            if (attachementfile == null)
            {
                return HttpNotFound();
            }
            return View(attachementfile);
        }

        // POST: /AttachementFile/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "AttachementFileID,FileName,ContentType,Content")] AttachementFile attachementfile)
        {
            if (ModelState.IsValid)
            {
                db.Entry(attachementfile).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(attachementfile);
        }

        // GET: /AttachementFile/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            AttachementFile attachementfile = await db.AttachmentFiles.FindAsync(id);
            if (attachementfile == null)
            {
                return HttpNotFound();
            }
            return View(attachementfile);
        }

        // POST: /AttachementFile/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            AttachementFile attachementfile = await db.AttachmentFiles.FindAsync(id);
            db.AttachmentFiles.Remove(attachementfile);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
